package com.cabrida.livestar.util;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;

import com.cabrida.livestar.BaseApplication;

/**
 * 网络 工具类
 * 
 */
public class NetworkUtils {
	/**
	 * 接受网络状态的广播Action
	 */
	public static final String NET_BROADCAST_ACTION = "com.network.state.action";

	public static final String NET_STATE_NAME = "network_state";

	/**
	 * Determine whether the network is effective
	 * 
	 * @param context
	 * @return if network is not null,return isAvailable;else return false
	 */
	public static boolean isNetAvailable(Context context) {
		if (null == context) {
			context = BaseApplication.getApplication();
		}
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo network = cm.getActiveNetworkInfo();
		if (network != null) {
			return network.isAvailable();
		}
		return false;
	}

	/**
	 * A unique identifier for each Android devices
	 * 
	 * @param activity
	 * @return
	 */
	public static String getUniqueCode(Activity activity) {
		return Secure.getString(activity.getContentResolver(),
				Secure.ANDROID_ID);
	}

	/**
	 * Get a phone Imei number
	 * 
	 * @param context
	 * @return deviceid
	 */
	public static String getImeiCode(Context context) {
		TelephonyManager tm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getDeviceId();
	}

	/**
	 * get network connection type,MOBILE or WIFI
	 * 
	 * @param context
	 * @return networkType
	 */
	public static String getNetType(Context context) {
		String networkType = null;
		try {
			ConnectivityManager cm = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo networkInfo = cm.getActiveNetworkInfo();// NULL

			if (networkInfo != null && networkInfo.isAvailable()) {
				networkType = networkInfo.getTypeName(); // MOBILE/WIFI
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return networkType;
	}

	/**
	 * get net work connection type :
	 * WIFI,2g(mobile,unit,cnet),3g(mobile,cnet,unit),and so on
	 * 
	 * @param context
	 * @return networkType
	 */
	public static String getNetDetailType(Context context) {
		String networkType = null;
		try {
			ConnectivityManager cm = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo networkInfo = cm.getActiveNetworkInfo();// NULL

			if (networkInfo != null && networkInfo.isAvailable()) {
				String typeName = networkInfo.getTypeName(); // MOBILE/WIFI
				if (!"MOBILE".equalsIgnoreCase(typeName)) {
					networkType = typeName;
				} else {
					networkType = networkInfo.getExtraInfo(); // cmwap/cmnet/wifi/uniwap/uninet
					if (networkType == null) {
						networkType = typeName + "#[]";
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return networkType;
	}

	/**
	 * Get a sim card information, operators
	 */

	/**
	 * get phone type : gsm or cdma and so on
	 */
	public static String getPhoneType(Context context) {
		TelephonyManager tpm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String phoneType = "";
		int type = tpm.getPhoneType();
		switch (type) {
		case TelephonyManager.PHONE_TYPE_GSM:
			phoneType = "GSM";
			break;
		case TelephonyManager.PHONE_TYPE_CDMA:
			phoneType = "CDMA";
			break;
		default:
			break;
		}
		return phoneType;
	}

	/**
	 * get sim card operator type
	 */
	public static String getSimOperator(Context context) {
		TelephonyManager tpm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String operator = "";
		if (tpm.getSimState() == TelephonyManager.SIM_STATE_READY)
			operator = tpm.getSimOperator();
		return operator;
	}

	/**
	 * get MAC address
	 */
	public static String getLocalMacAddress(Context context) {
		WifiManager wifi = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = wifi.getConnectionInfo();
		return info.getMacAddress();
	}

	/**
	 * 实时更新网络状态<br>
	 * -1为网络无连接<br>
	 * 1为WIFI<br>
	 * 2为移动网络<br>
	 */
	public static int CURRENT_NETWORK_STATE = -1;

	/**
	 * 判断网络是否连接
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isConnected(Context context) {

		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		if (null != connectivity) {

			NetworkInfo info = connectivity.getActiveNetworkInfo();
			if (null != info && info.isConnected()) {
				if (info.getState() == NetworkInfo.State.CONNECTED) {
					// 当前网络可用
					return true;
				}
			}
		}
		// 当前网络不可用
		return false;
	}

	/**
	 * 判断是否是wifi连接
	 */
	public static boolean isWifi(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		if (netInfo == null || !netInfo.isAvailable()) {
			return false;
		}
		if (ConnectivityManager.TYPE_WIFI == cm.getActiveNetworkInfo()
				.getType()) {
			return true;
		}
		return false;
	}

	/**
	 * 打开网络设置界面
	 */
	public static void openSetting(Activity activity) {
		Intent intent = new Intent("/");
		ComponentName cm = new ComponentName("com.android.settings",
				"com.android.settings.WirelessSettings");
		intent.setComponent(cm);
		intent.setAction("android.intent.action.VIEW");
		activity.startActivityForResult(intent, 0);
	}

	/**
	 * 开启服务,实时监听网络变化 需要自己在清单文件中配置服务<br>
	 * 然后把对应的Action传入<br>
	 * 服务类:android.develop.utils.net.NetService
	 */
	public static void startNetService(Context context, String action) {
		// 注册广播
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(NET_BROADCAST_ACTION);
		context.registerReceiver(mReceiver, intentFilter);
		// 开启服务
		Intent intent = new Intent();
		intent.setAction(action);
		context.bindService(intent, new ServiceConnection() {

			@Override
			public void onServiceDisconnected(ComponentName name) {

			}

			@Override
			public void onServiceConnected(ComponentName name, IBinder service) {

			}
		}, Context.BIND_AUTO_CREATE);
	}

	// 接受服务上发过来的广播
	private static BroadcastReceiver mReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent != null) {
				CURRENT_NETWORK_STATE = intent.getExtras().getInt(
						NET_STATE_NAME);
				switch (CURRENT_NETWORK_STATE) {
				case -1:
					// 网络更改为 无网络
					break;
				case 1:
					// 网络更改为 WIFI网络
					break;
				case 2:
					// 网络更改为 移动网络
					break;

				default:
					break;
				}
			}
		}
	};
}
